/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.crm.workorder.web.HolidayAction
   Module Description   :

   Date Created      : 2010/1/20
   Original Author   : andy.chuang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.crm.workorder.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import com.yaodian100.core.admin.utils.AdminHelper;
import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.entity.Option;
import com.yaodian100.crm.workorder.entity.Holiday;

/**
 * @author andy.chuang
 *
 */
public class HolidayAction {
    private String id;
    private String menuOptionId;
    private String name;
    private String day;
    private String dateFormat="yyyy/MM/dd";
    
    //for menu option
    private String code;
    
    @Resource
    private BaseManager baseMgr;
    private String iamlazy;
    private String AJAX;
    public String page(){
	if(AJAX!=null && AJAX.equals("true")){
	    return iamlazy+"_ajax";
	}else{
	    return iamlazy;
	}
    }
    private String returnPage(String orgPage){
	if(AJAX!=null && AJAX.equals("true")){
	    return orgPage+"_ajax";
	}else{
	    return orgPage;
	}
    }
    public String updateHoliday(){
	SimpleDateFormat sdf=new SimpleDateFormat(dateFormat);
	Date date=new Date();
	Holiday holiday=null;
	if(id!=null && id.length()>0){
	   holiday=(Holiday)baseMgr.get(Holiday.class, Long.parseLong(id)); 
	}else{
	    holiday=new Holiday();
	    holiday.setCreateUser(AdminHelper.getUserDetails().getUsername());
	    holiday.setCreateDate(date);
	   
	}
	if(menuOptionId!=null && menuOptionId.length()>0)
	holiday.setMenuOptionId(Long.parseLong(menuOptionId));
	holiday.setName(name);
	holiday.setModifyUser(AdminHelper.getUserDetails().getUsername());
	try {
	    holiday.setDay(sdf.parse(day));
	} catch (ParseException e) {
	   throw new RuntimeException(e);
	}
	baseMgr.saveOrUpdate(holiday);
	
	return returnPage("updateHoliday");
    }
    public String deleteHoliday(){
	if(id!=null && id.length()>0){
	    Holiday holiday=new Holiday();
	    holiday.setId(Long.parseLong(id));
	    baseMgr.delete(holiday);
	}
	return returnPage("deleteHoliday");
    }
    public String updateHolidayClass(){
	Option option=null;
	if(menuOptionId!=null && menuOptionId.length()>0){
	   option=(Option)baseMgr.get(Option.class, Long.parseLong(menuOptionId));
	}else{
	    Menu menu=(Menu)baseMgr.get(Menu.class, "WorkOrderHoliday.class");
	    option=new Option();
	    option.setMenu(menu);
	}
	option.setCode(code);
	option.setName(name);
	baseMgr.saveOrUpdate(option);
	return returnPage("updateHolidayClass");
    }
    public String deleteHolidayClass(){
	if(menuOptionId!=null && menuOptionId.length()>0){
	    Option option=new Option();
	    option.setOid(Long.parseLong(menuOptionId));
	    baseMgr.delete(option);
	}
	return returnPage("deleteHolidayClass");
    }
    /**
     * @return the id
     */
    public String getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }
    /**
     * @return the menuOptionId
     */
    public String getMenuOptionId() {
        return menuOptionId;
    }
    /**
     * @param menuOptionId the menuOptionId to set
     */
    public void setMenuOptionId(String menuOptionId) {
        this.menuOptionId = menuOptionId;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the day
     */
    public String getDay() {
        return day;
    }
    /**
     * @param day the day to set
     */
    public void setDay(String day) {
        this.day = day;
    }
    /**
     * @return the dateFormat
     */
    public String getDateFormat() {
        return dateFormat;
    }
    /**
     * @param dateFormat the dateFormat to set
     */
    public void setDateFormat(String dateFormat) {
        this.dateFormat = dateFormat;
    }
    /**
     * @return the code
     */
    public String getCode() {
        return code;
    }
    /**
     * @param code the code to set
     */
    public void setCode(String code) {
        this.code = code;
    }
    /**
     * @return the iamlazy
     */
    public String getIamlazy() {
        return iamlazy;
    }
    /**
     * @param iamlazy the iamlazy to set
     */
    public void setIamlazy(String iamlazy) {
        this.iamlazy = iamlazy;
    }
    /**
     * @return the aJAX
     */
    public String getAJAX() {
        return AJAX;
    }
    /**
     * @param aJAX the aJAX to set
     */
    public void setAJAX(String aJAX) {
        AJAX = aJAX;
    }

}
